package com.qiguliuxing.dts.admin.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.qiguliuxing.dts.admin.service.AdminPublishService;
import com.qiguliuxing.dts.core.util.JacksonUtil;
import com.qiguliuxing.dts.core.util.ResponseUtil;
import com.qiguliuxing.dts.db.domain.DtsPublishRecord;
import com.qiguliuxing.dts.db.service.DtsPublishRecordService;

@Service
public class AdminPublishService {
	@Autowired
	private DtsPublishRecordService dtsPublishService;

	public Object list(Integer userId, String publishSn, Integer page, Integer limit, String sort, String order) {
		List<DtsPublishRecord> publishList = dtsPublishService.querySelective(userId, publishSn, page, limit, sort,
				order);
		return ResponseUtil.ok(publishList);
	}

	public Object detail(Integer id) {
		DtsPublishRecord publishRecord = dtsPublishService.queryById(id);
		return ResponseUtil.ok(publishRecord);
	}

	public Object handle(String body) {
		Integer id = JacksonUtil.parseInteger(body, "id");
		// String handleResult = JacksonUtil.parseString(body, "handleResult");
		// if (id == null || StringUtils.isEmpty(handleResult)) {
		// return ResponseUtil.badArgument();
		// }

		DtsPublishRecord record = new DtsPublishRecord();
		record.setId(id);
		record.setStatus(1); // 已处理
		// record.setHandleResult(handleResult);

		if (dtsPublishService.updateById(record) == 0) {
			return ResponseUtil.updatedDataFailed();
		}
		return ResponseUtil.ok();
	}

	public Object delete(Integer id) {
		if (id == null) {
			return ResponseUtil.badArgument();
		}

		if (dtsPublishService.delete(id) == 0) {
			return ResponseUtil.updatedDataFailed();
		}
		return ResponseUtil.ok();
	}
}